package com.cn.xiaonuo.sys.modular.echarts.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.sys.modular.echarts.entity.Echarts;
import com.cn.xiaonuo.sys.modular.echarts.mapper.EchartsMapper;
import com.cn.xiaonuo.sys.modular.echarts.vo.EchartsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import java.util.List;

/**
 * @author ZhangYu
 * @Description:
 * @create 2021-03-03 17:59
 **/
@Slf4j
@RequestMapping("/echarts/")
@Controller
public class EchartsController {


    @Autowired
    private EchartsMapper echartsMapper;


    @GetMapping("/index")
    public String index(){
        return "business/echarts/index.html";
    }

    @GetMapping("/form")
    public String form() {
        return "business/echarts/form.html";
    }


    /**
     * 查询数据列表
     * @param echartsVo
     * @return
     */
    @GetMapping("page")
    @ResponseBody
    public PageResult<Echarts> findEchartsByPage(EchartsVo echartsVo){
        LambdaQueryWrapper<Echarts> queryWrapper= Wrappers.lambdaQuery();
        if (ObjectUtil.isNotNull(echartsVo)){
            if (!StringUtils.isEmpty(echartsVo.getName())){
                queryWrapper.like(Echarts::getName,echartsVo.getName());
            }
            if (echartsVo.getType()!=null){
                queryWrapper.eq(Echarts::getType,echartsVo.getType());
            }
        }
        Page<Echarts> echartsPage = echartsMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
        return new PageResult<>(echartsPage);
    }

    /**
     * 新增
     * @param echarts
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResponseData addEcharts(@RequestBody Echarts echarts){
        log.info("新增可视化参数=="+echarts);
        if (ObjectUtil.isNull(echarts)){
            return new SuccessResponseData();
        }
        echartsMapper.insert(echarts);
        return new SuccessResponseData();
    }

    @PostMapping("delete")
    @ResponseBody
    public ResponseData deleteEcharts(@RequestBody List<Echarts> echartsList){
        for (Echarts echarts : echartsList) {
            echartsMapper.deleteById(echarts.getId());
        }
        return new SuccessResponseData();
    }

    @PostMapping("edit")
    @ResponseBody
    public ResponseData editNews(@RequestBody Echarts echarts){
        echartsMapper.updateById(echarts);
        return new SuccessResponseData();
    }

    @GetMapping("detail")
    @ResponseBody
    public ResponseData detail( Echarts echarts){
        Echarts echarts1 = echartsMapper.selectById(echarts.getId());
        return new SuccessResponseData(echarts1);
    }

}
